###############################################################################
#
# Copyright (c) 2011 Rainmaker Entertainment
# All Rights Reserved.
#
# This file contains unpublished confidential and proprietary
# information of Rainmaker Entertainment.  The contents of this file
# may not be copied or duplicated, in whole or in part, by any
# means, electronic or hardcopy, without the express prior
# written permission of Rainmaker Entertainment.
#
#    $HeadURL: http://pt/src/trunk/maya/outsource/rnkOutsourceChecker/operations/geo.py $
#    $Revision: 67856 $
#    $Author: wmakari $
#    $Date: 2013-09-17 18:23:18 -0700 (Tue, 17 Sep 2013) $
#
###############################################################################

###############################################################################
## INFOS
###############################################################################

# Geo checks


###############################################################################
## MODULES
###############################################################################

import traceback
import re

try :
    import maya.cmds as cmds
    import maya.mel as mel
    import maya.OpenMaya as OpenMaya
except :
    traceback.print_exc()
    print "Can't load Maya modules."
    
    
import rnkAssetChecks.core as rnkAssetChecksCore
reload(rnkAssetChecksCore)

import rnkAssetChecks.geo as rnkAssetChecksGeo
reload(rnkAssetChecksGeo)

import core
reload(core)
        


###############################################################################
## FUNCTIONS
###############################################################################  

def suffixCheck(assetRoot, *args, **kw):
    """ 
    @brief Check if geometries transforms have a proper suffix.
    @return : List
    
    @param assetRoot - String - Asset root node.
    """
    
    badNodes = []
    suffixList = ['_HI', '_EX', '_GRO', '_LO']
    
    shapesList = rnkAssetChecksCore.listShapes(assetRoot)
    if not len(shapesList) == 0 :
        for eachShapes in shapesList :
            trfs = cmds.listRelatives(eachShapes, parent=True)[0]
            if not trfs in badNodes :
                good = False
                for eachSuffix in suffixList :
                    if trfs.endswith(eachSuffix) is True :
                        good = True
                        break
                
                if good is False :
                    badNodes.append(trfs)
                    
                    
    if len(badNodes) == 0 :    
        return core.checkResult(allowPublish=True, msg="Everything is OK")
    else :           
        return core.checkResult(allowPublish=False, msg="These geometries have a bad suffix", nodes=badNodes)

suffixCheck.modifiedScene = False
suffixCheck.uiName        = 'Check Geometries Suffix'
suffixCheck.revision      = "$Revision: 67856 $".split()[1]



def checkGeoVisibility(assetRoot, *args, **kw):
    """ 
    @brief Check if every geometries transforms and shapes are visible.
    @return : List
    
    @param assetRoot - String - Asset root node.
    """
    
    badNodes = []
    
    shapesList = rnkAssetChecksCore.listShapes(assetRoot)
    if not len(shapesList) == 0 :
        for eachShapes in shapesList :
            if cmds.getAttr('%s.visibility' % eachShapes) is False :
                badNodes.append(eachShapes)
                
            trfs = cmds.listRelatives(eachShapes, parent=True)[0]
            if not trfs in badNodes and cmds.getAttr('%s.visibility' % trfs) is False :
                badNodes.append(trfs)
                
                
    if len(badNodes) == 0 : 
            return core.checkResult(allowPublish=True, msg="Everything is OK")
    else :           
            return core.checkResult(allowPublish=False, msg="These geometries transforms or/and shapes are not visible", nodes=badNodes)

checkGeoVisibility.modifiedScene = False
checkGeoVisibility.uiName        = 'Check Geometries Visibility'
checkGeoVisibility.revision      = "$Revision: 67856 $".split()[1]



def checkHistory(assetRoot, *args, **kw):
    """ 
    @brief Check if the geometries shapes have an input history.
    @return : List
    
    @param assetRoot - String - Asset root node.
    """
    
    issues = []

    nodes = rnkAssetChecksCore.listShapes(assetRoot, types=["mesh"])
    if not len(nodes) == 0 :
        for shape in nodes:
    
            if cmds.objExists('%s.__rnkDoNotDeleteHistory' % shape) is True :
                pass
            else :
                if cmds.nodeType(shape) == 'mesh' :
                    input = "inMesh"
        
                elif cmds.nodeType(shape)  == 'nurbsSurface' :
                    input = "create"
    
                if cmds.listConnections('%s.%s' % (shape, input), s=True, d=False):
                    issues.append(shape)

    if len(issues) == 0 : 
            return core.checkResult(allowPublish=True, msg="Everything is OK")
    else :           
            return core.checkResult(allowPublish=False, msg="These geometries shapes have an input history", nodes=issues)

checkHistory.modifiedScene = False
checkHistory.uiName        = 'Check Geometries Input History'
checkHistory.revision      = "$Revision: 67856 $".split()[1]




def checkEmptyShapes(assetRoot, *args, **kw):
    
    issues = rnkAssetChecksGeo.checkEmptyShapes(assetRoot)
                
    if len(issues) == 0 : 
            return core.checkResult(allowPublish=True, msg="Everything is OK")
    else :           
            return core.checkResult(allowPublish=False, msg="These shapes are empty", nodes=issues)

checkEmptyShapes.modifiedScene = False
checkEmptyShapes.uiName        = 'Check Empty Shapes'
checkEmptyShapes.revision      = "$Revision: 67856 $".split()[1]
checkEmptyShapes.__doc__       = rnkAssetChecksGeo.checkEmptyShapes.__doc__


def checkUV(assetRoot, *args, **kw):
    
    issues = rnkAssetChecksGeo.checkUV(assetRoot)
                
    if len(issues) == 0 : 
            return core.checkResult(allowPublish=True, msg="Everything is OK")
    else :           
            return core.checkResult(allowPublish=False, msg="Those geometries don't have UVs", nodes=issues)

checkUV.modifiedScene = False
checkUV.uiName        = 'Check UVs'
checkUV.revision      = "$Revision: 67856 $".split()[1]
checkUV.__doc__       = rnkAssetChecksGeo.checkUV.__doc__



def checkNamesEndWithNumber(assetRoot, *args, **kw):
    
    issues = rnkAssetChecksGeo.checkNamesEndWithNumber(assetRoot)
                
    if len(issues) == 0 :  
            return core.checkResult(allowPublish=True, msg="Everything is OK")
    else :           
            return core.checkResult(allowPublish=False, msg="Those nodes end with a number", nodes=issues)

checkNamesEndWithNumber.modifiedScene = False
checkNamesEndWithNumber.uiName        = 'Check Names end With Number'
checkNamesEndWithNumber.revision      = "$Revision: 67856 $".split()[1]
checkNamesEndWithNumber.__doc__       = rnkAssetChecksGeo.checkNamesEndWithNumber.__doc__



def checkPivot(assetRoot, *args, **kw):

    issues = rnkAssetChecksGeo.checkPivot(assetRoot)
                
    if len(issues) == 0 :    
            return core.checkResult(allowPublish=True, msg="Everything is OK")
    else :           
            return core.checkResult(allowPublish=False, msg="Those geometries have non-zeroed-out pivot points", nodes=issues)

checkPivot.modifiedScene = False
checkPivot.uiName        = 'Check Pivots'
checkPivot.revision      = "$Revision: 67856 $".split()[1]
checkPivot.__doc__       = rnkAssetChecksGeo.checkPivot.__doc__



def isMeshTweaked(assetRoot, *args, **kw):

    issues = rnkAssetChecksGeo.isMeshTweaked(assetRoot)
                
    if len(issues) == 0 : 
            return core.checkResult(allowPublish=True, msg="Everything is OK")
    else :           
            return core.checkResult(allowPublish=False, msg="Those meshes have vertices who are not 0", nodes=issues)

isMeshTweaked.modifiedScene = False
isMeshTweaked.uiName        = 'Check Tweaked Meshes'
isMeshTweaked.revision      = "$Revision: 67856 $".split()[1]
isMeshTweaked.__doc__       = rnkAssetChecksGeo.isMeshTweaked.__doc__



def checkQuadPolygons(assetRoot, *args, **kw):

    
    issues = rnkAssetChecksGeo.checkQuadPolygons(assetRoot)
                
    if len(issues) == 0 :  
            return core.checkResult(allowPublish=True, msg="Everything is OK")
    else :           
            return core.checkResult(allowPublish=False, msg="Those meshes have faces with more then 4 edges", nodes=issues)

checkQuadPolygons.modifiedScene = False
checkQuadPolygons.uiName        = 'Check Quad Polygons'
checkQuadPolygons.revision      = "$Revision: 67856 $".split()[1]
checkQuadPolygons.__doc__       = rnkAssetChecksGeo.checkQuadPolygons.__doc__



def checkFacesWithHoles(assetRoot, *args, **kw):

    issues = rnkAssetChecksGeo.checkFacesWithHoles(assetRoot)
                
    if len(issues) == 0 : 
            return core.checkResult(allowPublish=True, msg="Everything is OK")
    else :           
            return core.checkResult(allowPublish=False, msg="Those meshes have faces with holes", nodes=issues)

checkFacesWithHoles.modifiedScene = False
checkFacesWithHoles.uiName        = 'Check Faces With Holes'
checkFacesWithHoles.revision      = "$Revision: 67856 $".split()[1]
checkFacesWithHoles.__doc__       = rnkAssetChecksGeo.checkFacesWithHoles.__doc__



def checkZeroLengthEdges(assetRoot, *args, **kw):

    issues = rnkAssetChecksGeo.checkZeroLengthEdges(assetRoot)
                
    if len(issues) == 0 : 
            return core.checkResult(allowPublish=True, msg="Everything is OK")
    else :           
            return core.checkResult(allowPublish=False, msg="These edges have zero or near zero length", nodes=issues)

checkZeroLengthEdges.modifiedScene = False
checkZeroLengthEdges.uiName        = 'Check Zero-Length Edges'
checkZeroLengthEdges.revision      = "$Revision: 67856 $".split()[1]
checkZeroLengthEdges.__doc__       = rnkAssetChecksGeo.checkZeroLengthEdges.__doc__




def checkNonmanifoldGeo(assetRoot, *args, **kw):
    
    issues = rnkAssetChecksGeo.checkNonmanifoldGeo(assetRoot)
                
    if len(issues) == 0 : 
            return core.checkResult(allowPublish=True, msg="Everything is OK")
    else :           
            return core.checkResult(allowPublish=False, msg="Those geometries have manifold", nodes=issues)

checkNonmanifoldGeo.modifiedScene = False
checkNonmanifoldGeo.uiName        = 'Check Non-Manifold Geometries'
checkNonmanifoldGeo.revision      = "$Revision: 67856 $".split()[1]
checkNonmanifoldGeo.__doc__       = rnkAssetChecksGeo.checkNonmanifoldGeo.__doc__


def checkTrianglesSharingVtx(assetRoot, *args, **kw):
    
    issues = rnkAssetChecksGeo.checkTrianglesSharingVtx(assetRoot)
                
    if len(issues) == 0 : 
            return core.checkResult(allowPublish=True, msg="Everything is OK")
    else :           
            return core.checkResult(allowPublish=True, msg="Those triangle faces share the same vertex", nodes=issues, warning=True)

checkTrianglesSharingVtx.modifiedScene = False
checkTrianglesSharingVtx.uiName        = 'Check Triangle Faces Vertex Sharing'
checkTrianglesSharingVtx.revision      = "$Revision: 67856 $".split()[1]
checkTrianglesSharingVtx.__doc__       = rnkAssetChecksGeo.checkTrianglesSharingVtx.__doc__


def useOldStyleSubdivs(assetRoot, *args, **kw):
    
    issues = rnkAssetChecksGeo.useOldStyleSubdivs(assetRoot)

    return core.checkResult(allowPublish=True, msg="Everything is OK")


useOldStyleSubdivs.modifiedScene = True
useOldStyleSubdivs.uiName        = 'Use Old-Style SubDiv'
useOldStyleSubdivs.revision      = "$Revision: 67856 $".split()[1]
useOldStyleSubdivs.__doc__       = rnkAssetChecksGeo.useOldStyleSubdivs.__doc__